﻿using System;
using BizUnit;
using BizUnit.BizUnitOM;
using System.IO;
using System.Diagnostics;
using BTS.Testing.TraceMonitor.BizUnitSteps.TraceControlAgent;

namespace BTS.Testing.TraceMonitor.BizUnitSteps
{
    /// <summary>
    /// Starts the event test trace
    /// </summary>
    public class StartRealtimeEventTrace :ITestStepOM 
    {
        public string TraceToolPath { get; set; }
        /// <summary>
        /// Executes the bizunit step
        /// </summary>
        /// <param name="testConfig"></param>
        /// <param name="context"></param>
        public void Execute(System.Xml.XmlNode testConfig, Context context)
        {
            TraceToolPath = context.ReadConfigAsString(testConfig, "TraceToolPath");      
            Execute(context);
        }

        public void Execute(Context context)
        {
            Execute();
        }
        public void Execute()
        {
            if(string.IsNullOrEmpty(TraceToolPath) || !File.Exists(TraceToolPath))
                throw new ApplicationException("Please supply a valid path to the trace tool");

            TraceToolPath = Path.GetFullPath(TraceToolPath);

            var process = new Process();
            var processStartInfo = new ProcessStartInfo { FileName = TraceToolPath, WorkingDirectory = Path.GetDirectoryName(TraceToolPath) };
            process.StartInfo = processStartInfo;
            process.Start();

            var serviceAgent = new TraceControlServiceClient();
            serviceAgent.Start();
        }

        public void Validate(Context context)
        {
            
        }
    }
}
